package com.situ.company.service.impl;

import java.util.List;

import com.situ.company.dao.IUserDao;
import com.situ.company.dao.IUserRoleDao;
import com.situ.company.dao.impl.UserDaoImpl;
import com.situ.company.dao.impl.UserRoleDaoImpl;
import com.situ.company.entity.User;
import com.situ.company.service.IUserService;
import com.situ.company.util.JSONResult;
import com.situ.company.vo.PageInfo;
import com.situ.company.vo.UserInfoVO;

public class UserServiceImpl implements IUserService{
	private IUserDao userDao = new UserDaoImpl();
	private IUserRoleDao userRoleDao = new UserRoleDaoImpl();

	@Override
	public List<User> selectAll() {
		List<User> list = userDao.selectAll();
		for (User user : list) {
			user.setName("2020-" + user.getName());
		}
		return list;
	}

	@Override
	public boolean deleteById(int id) {
		int count = userDao.deleteById(id);
		// 在users表中根据id删除了这个员工之后，要去user_role表里面根据user_id删除这个员工所有的角色
		userRoleDao.deleteByUserId(id);
		
		return count == 1;
	}

	@Override
	public PageInfo selectByPage(int pageNo, int pageSize) {
		// 在Service封装PageInfo，涉及到两条查询语句
		// 1、查询当前页的数据
//		int offset = (pageNo - 1) * pageSize;
//		List<UserInfoVO> list = userDao.selectByPage(offset, pageSize);
//		
//		// 2、查询一共有多少条数据,目的是为了算出一共有多少页
//		int totalCount = userDao.selectTotalCount();
//		int totalPage = (int)Math.ceil((double)totalCount / pageSize);
		
		PageInfo pageInfo = null ;//new PageInfo(pageNo, pageSize, list, totalPage);
		return pageInfo;
	}
	
	@Override
	public JSONResult selectLayuiByPage(String name, int page, int limit) {
		// 1、查询当前页的数据
		int offset = (page - 1) * limit;
		List<UserInfoVO> list = userDao.selectByPage(name, offset, limit);
		
		// 2、查询一共有多少条数据,目的是为了算出一共有多少页
		int totalCount = userDao.selectTotalCount(name);
		
		JSONResult jsonResult = new JSONResult(0, "", list, totalCount);
		return jsonResult;
	}
	

	@Override
	public User login(String name, String password) {
		return userDao.login(name, password);
	}

	public static void main(String[] args) {
		System.out.println(7 / 3);// 2
		System.out.println(Math.ceil(7 / 3));// 2.0
		System.out.println((double)7 / 3);
		System.out.println(Math.ceil((double)7 / 3));
	}

	@Override
	public boolean insert(User user, String[] roleIds) {
		int id = userDao.insert(user);
		for (String roleId : roleIds) {
			userRoleDao.insert(id, Integer.parseInt(roleId));
		}
		
		
		return true;
	}

	@Override
	public boolean deleteAll(String[] ids) {
		int count = 0;
		for (String id : ids) {
			 count += userDao.deleteById(Integer.parseInt(id));
		}
		// 2、delete from users where id in(3,4,5);
		// delete from users where id=3 or id=4 or id=5;
		// delete from users where id=? or id=? or id=?;
		// mybatis 
		return count == ids.length;
	}

	@Override
	public User selectById(int id) {
		return userDao.selectById(id);
	}

	@Override
	public boolean update(User user, String[] roleIds) {
		int id = userDao.update(user);
		userRoleDao.deleteByUserId(user.getId());
		for (String roleId : roleIds) {
			userRoleDao.insert(user.getId(), Integer.parseInt(roleId));
		}
		
		return true;
	}

	
}
